<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

class Editpodb extends HTML_QuickForm {
  private $tpl;
  private $conf;

  function __construct($tpl,$conf) {
    $this->tpl = $tpl;
    $this->conf = $conf;

    if($this->tpl->get_template_vars('modify')) {
      $po = $this->conf->getPo($this->conf->getPodb_id());
      $defaults = array('msgid'  => $po['msgid'],
                        'msgstr' => $po['msgstr']);
      $this->setDefaults($defaults);
    }

    parent::HTML_QuickForm('editpodb');
    $this->addElement('header', 'header', 'addrole');
    $this->addElement('text', 'msgid', _('msgid'), array('size' => 50, 'maxlength' => 255));
    $this->addElement('text', 'msgstr', _('msgstr'), array('size' => 50, 'maxlength' => 255));
    $this->addElement('submit', 'btn_Submit', _('submit'), array('class' => 'submit', 'onmouseover' => 'this.style.backgroundColor = \'#E0E7E7\'; this.style.borderColor = \'#728FA4\';', 'onmouseout' => 'this.style.backgroundColor = \'#eeeeee\'; this.style.borderColor = \'#999999\';'));
    $this->applyFilter('__ALL__', 'trim');
    $this->registerRule('check_exists', 'function', 'checkExists', $this);
    $this->addRule('msgid', _('msgid required'), 'required');
    $this->addRule('msgstr', _('msgstr required'), 'required');
    $this->addRule('msgid', _('msgid exists'), 'check_exists');
  }

  function checkExists() {
    $this->conf->setMsgid($this->getSubmitValue('msgid'));
     if($this->tpl->get_template_vars('modify')) {
       return $this->conf->checkExistsPodb($op = 'modify');
     } else {
       return $this->conf->checkExistsPodb($op = 'add');
     }
  }

  function displayForm() {
    $this->renderer =& new HTML_QuickForm_Renderer_ArraySmarty($this->tpl);
    $this->renderer->setRequiredTemplate(
  		    '{if $error}
  		 	     <font color="red">{$label}</font>
  	 	     {else}
  	 		     {$label}
  		 	     {if $required}
  	            <font color="red" size="1">*</font>
  	   	     {/if}
  	       {/if}'
    );
    $this->renderer->setErrorTemplate(
          '{if $error}
  	  	    <font color="blue" size="1">{$error}</font><br />
  	       {/if}{$html}'
    );
    $this->setRequiredNote(
   		'<small class="red">*&nbsp;</small><small class="blue">'._('required fields').'</small>'
   	);
   	$this->accept($this->renderer);
   	$this->tpl->assign('form_data',$this->renderer->toArray());
   	$this->tpl->assign('msg',$this->conf->getMsg());
  }

  function process() {
    if($this->tpl->get_template_vars('modify')) {
      $this->conf->modifyMsgid($this->getSubmitValues());
    } else {
      $this->conf->addMsgid($this->getSubmitValues());
    }
    $this->tpl->assign('result',true);
    $this->tpl->assign('msg',$this->conf->getMsg());
  }

} // end of class

?>